# demo06_tarfile.py
import tarfile
"""
# 1.创建并写入tar文件
# 设置压缩方式 w:gz     w:bz2
with tarfile.open('/opt/mytest/demo.tar.gz', mode='w:gz') as tar:
    # 添加文件
    tar.add('/opt/a.log')
    tar.add('/opt/b.log')
    # 添加目录
    tar.add('/root/NSD2408/day07')
# 2.读取tar文件
with tarfile.open('/opt/mytest/demo.tar.gz', mode='r') as tar:
    # 读取所有的
    files = tar.getmembers()
    # print(files)
    for item in files:
        print(item.name)
"""
# 3.提取文件
with tarfile.open('/opt/mytest/demo.tar.gz', mode='r') as tar:
    # 提取所有的文件到指定的目录下
    tar.extractall(path='/opt/mytest')


